//
//  WaitForPayDetailViewController.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 19/04/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "WaitForPayDetailViewController.h"

@interface WaitForPayDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * detailTableView;
    NSArray * formTableViewList;
}
@end

@implementation WaitForPayDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString * path = [[NSBundle mainBundle]pathForResource:@"WaitForPayTable" ofType:@"plist"];
    formTableViewList = [[NSArray alloc]initWithContentsOfFile:path];
    
    __weak WaitForPayDetailViewController *  weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"待付款详情",@"title", nil] leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    UIView * bottomView = [self addBottomView];
    
    detailTableView = [self addTableViewInView:self.view frame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (navView.frame.origin.y + navView.frame.size.height) - bottomView.frame.size.height) delegateAndDataSource:self];
}



-(UIView *)addBottomView{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 45, self.view.frame.size.width, 45)];
    [self.view addSubview:bgView];
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, bgView.frame.size.width / 2, bgView.frame.size.height);
    leftButton.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftButton setTitle:@"提醒用户付" forState:UIControlStateNormal];
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, leftButton.frame.size.width / 2  - 100, 0, 0)];
    [leftButton setImage:[UIImage imageNamed:@"alarm_icon"] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:leftButton];
    
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(bgView.frame.size.width / 2, 0, bgView.frame.size.width / 2, bgView.frame.size.height);
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitle:@"网点打印" forState:UIControlStateNormal];
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, leftButton.frame.size.width / 2  - 100, 0, 0)];
    [rightButton setImage:[UIImage imageNamed:@"wang_diang"] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.backgroundColor = UIColorFromRGB(0x20C67A);
    [bgView addSubview:rightButton];
    
    return bgView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if ([indexPath section] == 0) {
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
        bgView.backgroundColor = [UIColor whiteColor];
        [cell addSubview:bgView];
        
        UIImageView * timeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 60, 60)];
        timeImgView.layer.cornerRadius = 30.0;
        timeImgView.layer.masksToBounds = YES;
        timeImgView.backgroundColor = [UIColor blueColor];
        [bgView addSubview:timeImgView];
        
        UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(timeImgView.frame.origin.x + timeImgView.frame.size.width + 5, timeImgView.frame.origin.y, 200, 20)];
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = [UIColor colorWithRed:51.0 / 255.0 green:51.0 / 255.0 blue:51.0 / 255.0 alpha:1.0];
        [bgView addSubview:nameLabel];
        
        UILabel * addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height + 10, bgView.frame.size.width - timeImgView.frame.origin.x - timeImgView.frame.size.width - 20, nameLabel.frame.size.height)];
        addressLabel.font = [UIFont systemFontOfSize:14];
        addressLabel.textColor = [UIColor colorWithRed:148.0 / 255.0 green:148.0 / 255.0 blue:148.0 / 255.0 alpha:1.0];
        
        [bgView addSubview:addressLabel];
        
        
        UIButton * phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        phoneButton.frame = CGRectMake(bgView.frame.size.width - timeImgView.frame.size.width - timeImgView.frame.origin.x, timeImgView.frame.origin.y, timeImgView.frame.size.width, timeImgView.frame.size.height);
        phoneButton.layer.cornerRadius = phoneButton.frame.size.height / 2;
        phoneButton.backgroundColor = [UIColor blueColor];
        [bgView addSubview:phoneButton];
        
#if kDevelopment
        addressLabel.text = @"金城社区东花市里20号楼18楼";
        
        nameLabel.text = @"张先生";
#endif
        
        bgView.frame = CGRectMake(bgView.frame.origin.x, bgView.frame.origin.y, bgView.frame.size.width, timeImgView.frame.size.height + timeImgView.frame.origin.y + timeImgView.frame.origin.y);
        cell.cellHeight = bgView.frame.size.height + bgView.frame.origin.y;
    }else if([indexPath section] == 1){
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, tableView.frame.size.width, 35)];
        label.text = @"订单信息";
        [cell addSubview:label];
        cell.cellHeight = label.frame.size.height + label.frame.origin.y * 2;
        
        UIButton * copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [copyBtn setTitle:@"复制" forState:UIControlStateNormal];
        copyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [copyBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        copyBtn.frame = CGRectMake(tableView.frame.size.width - 50 - 20, (cell.cellHeight - 18) / 2, 50, 18);
        copyBtn.layer.cornerRadius = 6.0;
        copyBtn.layer.borderColor = UIColorFromRGB(0x333333).CGColor;
        copyBtn.layer.borderWidth = 0.5;
        [cell addSubview:copyBtn];
        
        
        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, cell.cellHeight - 1, tableView.frame.size.width, 0.5)];
        lineView.backgroundColor = [UIColor colorWithRed:228.0 / 255.0 green:228.0 / 255.0 blue:228.0 / 255.0 alpha:1.0];
        [cell addSubview:lineView];
    }else if([indexPath section] == 2){
        NSDictionary * dic = [formTableViewList objectAtIndex:[indexPath row]];
        UILabel * nameLabel = [[UILabel alloc] init];
        nameLabel.frame = CGRectMake(20, 10, 0, 25);
        nameLabel.text = [NSString stringWithFormat:@"%@:",[dic objectForKey:@"name"]];
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = UIColorFromRGB(0x999999);
        [nameLabel enlargeWidthToKeepFontSize];
        [cell addSubview:nameLabel];
        
        
        UILabel * valueLabel = [[UILabel alloc] init];
        valueLabel.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.frame.size.width + 5, nameLabel.frame.origin.y, tableView.frame.size.width - nameLabel.frame.origin.x - nameLabel.frame.size.width - 20,0);
        
        valueLabel.font = [UIFont systemFontOfSize:14];
        valueLabel.textColor = UIColorFromRGB(0x333333);
        [cell addSubview:valueLabel];
#if kDevelopment
        valueLabel.text = @"test";
#endif
        [valueLabel enlargeHeightToKeepFontSizeWithMinHeight:nameLabel.frame.size.height];
        
        cell.cellHeight = valueLabel.frame.size.height + valueLabel.frame.origin.y * 2;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 1;
    }else if(section == 2){
        return [formTableViewList count];
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
        v.backgroundColor = UIColorFromRGB(0xE9E9E9);
        return v;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 15;
    }
    return 0;
}

@end
